//
//  TopicPingViewController.m
//  A_Object_Netease
//
//  Created by 张春洋  on 16/1/20.
//  Copyright © 2016年 张春洋 . All rights reserved.
//
#import "TopicPingViewController.h"
#import "TopicPingModel.h"
#import "TopicPingTableViewCell.h"
#import "LMTTImeChuo.h"
#import <UIImageView+WebCache.h>
#import "QPNetWorkingTool.h"
#import "TimeChange.h"

#define KUAN [UIScreen mainScreen].bounds.size.width /375
#define GAO [UIScreen mainScreen].bounds.size.height /667


@interface TopicPingViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tPTableView;
@property (nonatomic, retain) NSDictionary *touDic;
@property (nonatomic, retain) NSArray *remenArr;
@property (nonatomic, retain) NSArray *zuijinArr;
@property (nonatomic, retain) UISegmentedControl *seg;

@end

@implementation TopicPingViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.alpha = 0;
    self.navigationItem.title = @"话题详情";
    // tableView
    [self creatTableView];
    // 数据
    [self data];
    
}

#pragma mark - 数据请求
- (void)data
{
    NSString *userId = self.str;
    NSLog(@"%@", self.str);
    // id拼接
    NSString *urlStr = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/qa/%@.html", userId];
    [QPNetWorkingTool getNetWorkingTool:urlStr dic:nil success:^(id result) {
        self.touDic = result[@"data"][@"expert"];
        self.remenArr = result[@"data"][@"hotList"];
        self.zuijinArr = result[@"data"][@"latestList"];
        [self hander];
        
        [self.tPTableView reloadData];
    } fail:^(NSError *error) {
        
    }];
}

// 详情页上方的布局
- (void)hander
{
    UIView *hand = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300 *GAO)];
    // 最上边的图
    UIImageView *tupian = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180 * GAO)];

//    tupian.backgroundColor = [UIColor redColor];
    // 本人信息
    UIImageView *benren = [[UIImageView alloc] initWithFrame:CGRectMake(10 *KUAN, 185 *GAO, self.view.frame.size.width - 20, 80 *GAO)];
//    benren.backgroundColor = [UIColor magentaColor];
    // 本人头像
    UIImageView *touxiang = [[UIImageView alloc] initWithFrame:CGRectMake(10 *KUAN, GAO * 10, KUAN * 30, GAO * 30)];
    [touxiang sd_setImageWithURL:[NSURL URLWithString:_touDic[@"headpicurl"]]];
    // 本人的名字
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(KUAN * 50, GAO * 10, KUAN * 100, GAO * 20)];
    name.font = [UIFont systemFontOfSize:12];
    name.text = [NSString stringWithFormat:@"%@|%@", _touDic[@"name"], _touDic[@"title"]];
    // 简介
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(50 * KUAN, GAO * 30, benren.frame.size.width - 60, benren.frame.size.height - 30)];
    title.text = _touDic[@"description"];
    title.font = [UIFont systemFontOfSize:14];
    title.numberOfLines = 0;
    // 提问人数
    UILabel *tiWen = [[UILabel alloc] initWithFrame:CGRectMake(15 *KUAN, GAO * 270, KUAN * 200,GAO * 25)];
//    tiWen.backgroundColor = [UIColor purpleColor];
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"", @""]];
    _seg.frame = CGRectMake(260 *KUAN,GAO* 270, KUAN * 80, GAO * 25);
    _seg.selectedSegmentIndex = 0;
    _seg.alpha = 0;
    
    
    [tupian sd_setImageWithURL:[NSURL URLWithString:_touDic[@"picurl"]]];
    [benren addSubview:touxiang];
    [benren addSubview:name];
    [benren addSubview:title];
    [hand addSubview:_seg];
    [hand addSubview:tiWen];
    [hand addSubview:benren];
    [hand addSubview:tupian];
    [self.tPTableView.tableHeaderView addSubview:hand];
    
   }

#pragma mark - 创建tableView
- (void)creatTableView
{
    // 创建tableView
    self.tPTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    // 签订协议
    _tPTableView.delegate = self;
    _tPTableView.dataSource = self;
    // tableView左侧滑动条与cell分割线隐藏
    _tPTableView.showsVerticalScrollIndicator = NO;
    _tPTableView.separatorStyle = NO;
    _tPTableView.contentInset = UIEdgeInsetsMake(-20 , 0,  59, 0);
    // 创建tableView的头部 用于加轮播图
    UIView *hander = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 300 *GAO)];
//    hander.backgroundColor = [UIColor greenColor];
    _tPTableView.tableHeaderView = hander;
    [self.view addSubview:_tPTableView];
    [_tPTableView registerClass:[TopicPingTableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

#pragma mark - tableView的协议方法实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_seg.selectedSegmentIndex == 0) {
        return _zuijinArr.count;
    } else {
        return _remenArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_seg.selectedSegmentIndex == 0) {
        NSDictionary *zuiJinDic = _zuijinArr[indexPath.row];
        TopicPingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        [cell.userTou sd_setImageWithURL:[NSURL URLWithString:zuiJinDic[@"question"][@"userHeadPicUrl"]]placeholderImage:[UIImage imageNamed:@"aa23f04ca40c73d192afe06d498277b8"]];
        cell.userMing.text = zuiJinDic[@"question"][@"userName"];
        cell.question.text = zuiJinDic[@"question"][@"content"];
        [cell.tou sd_setImageWithURL:[NSURL URLWithString:zuiJinDic[@"answer"][@"specialistHeadPicUrl"]]placeholderImage:[UIImage imageNamed:@"aa23f04ca40c73d192afe06d498277b8"]];
        cell.name.text = zuiJinDic[@"answer"][@"specialistName"];
        cell.answer.text = zuiJinDic[@"answer"][@"content"];
        NSNumber *time = zuiJinDic[@"question"][@"cTime"];
        NSNumber *str = @(time.integerValue / 1000);
        cell.time.text = [TimeChange compareCurrentTime:str];
        return cell;
    } else {
        NSDictionary *remenDic = _remenArr[indexPath.row];
        TopicPingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        [cell.userTou sd_setImageWithURL:[NSURL URLWithString:remenDic[@"question"][@"userHeadPicUrl"]]placeholderImage:[UIImage imageNamed:@"aa23f04ca40c73d192afe06d498277b8"]];
        cell.userMing.text = remenDic[@"question"][@"userName"];
        cell.question.text = remenDic[@"question"][@"content"];
        [cell.tou sd_setImageWithURL:[NSURL URLWithString:remenDic[@"answer"][@"specialistHeadPicUrl"]]placeholderImage:[UIImage imageNamed:@"aa23f04ca40c73d192afe06d498277b8"]];
        cell.name.text = remenDic[@"answer"][@"specialistName"];
        cell.answer.text = remenDic[@"answer"][@"content"];
        NSNumber *time = remenDic[@"question"][@"cTime"];
        NSNumber *str = @(time.integerValue / 1000);
        cell.time.text = [TimeChange compareCurrentTime:str];
        return cell;
    }
}

// cell高度设置
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_seg.selectedSegmentIndex == 0) {
        NSDictionary *zuiJinDic = _zuijinArr[indexPath.row];
        CGFloat h = [TopicPingTableViewCell heightWihtText:zuiJinDic[@"question"][@"content"]];
        CGFloat r = [TopicPingTableViewCell heightWihtText:zuiJinDic[@"answer"][@"content"]];
        return (h + r + 150) * GAO;
    } else {
    NSDictionary *remenDic = _remenArr[indexPath.row];
    CGFloat h = [TopicPingTableViewCell heightWihtText:remenDic[@"question"][@"content"]];
    CGFloat r = [TopicPingTableViewCell heightWihtText:remenDic[@"answer"][@"content"]];
    return (h + r + 150) * GAO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
